﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.Config;
using Dict.Blo;
using Dict.DataModel;

namespace UI
{
    public class ArrayPosition : MonoBehaviour
    {

        public UILabel heroName;
        public UILabel heroLevel;
        public UILabel tipLabel;
        public GameObject emptyIcon;
        public GameObject lockLogo;
        public GameObject addLogo;
        public int posIndex;

        HeroIcon heroIcon;

        public void UpdatePosition(UIOtherPlayerData data)
        {
            if(data != null)
            {
                //初始化阵容人物icon
                Dictionary<int, UIHeroFormation> battleCamps = data.formations;
                UIHeroFormation formation = battleCamps[posIndex];
                if (formation.hero != null)
                {
                    TableHero heroData = DictHeroBlo.GetTableHero(formation.hero.heroId);
                    if(heroData.heroClass == (int)HeroClass.Main_Actor_Man || heroData.heroClass == (int)HeroClass.Main_Actor_Woman)
                        SetHero(formation.hero, data.playerName);
                    else
                        SetHero(formation.hero, formation.hero.heroName);
                }
                else
                {
                    SetEmpty(formation.UnlockLevel);
                }
            }
            else
            {
                //初始化阵容人物icon
                Dictionary<int, UIHeroFormation> battleCamps = StaticData.playerData.InstPlayerData.BattleCamps;
                if (battleCamps[posIndex].hero != null)
                {
                    SetHero(battleCamps[posIndex].hero, battleCamps[posIndex].hero.heroName);
                }
                else
                {
                    SetEmpty(battleCamps[posIndex].UnlockLevel);
                }
            }
        }
        void SetHero(UIHero hero, string name)
        {
            heroName.gameObject.SetActive(true);
            heroLevel.gameObject.SetActive(true);
            tipLabel.gameObject.SetActive(false);

            heroName.text = name;
            
            heroLevel.text = TableTextBlo.GetText("level") + hero.level.ToString();

            if(heroIcon == null)
            {
                float scale = (float)70 / 90;
                GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/HeroIcon") as GameObject;
                GameObject icon = Instantiate(obj);
                Destroy(icon.GetComponent<BoxCollider>());
                icon.transform.parent = this.gameObject.transform;
                icon.transform.localPosition = new Vector3(-50, 0, 0);
                icon.transform.localScale = new Vector3(scale, scale, scale);
                heroIcon = icon.GetComponent<HeroIcon>();
            }
            heroIcon.gameObject.SetActive(true);
            heroIcon.SetHeroWithId(hero.heroId);
            if(hero.talentLevel > 0)
            {
                heroIcon.UpdateRefine("+" + hero.talentLevel.ToString());
            }
            else
            {
                heroIcon.UpdateRefine("");
            }
        }
        
        void SetEmpty(int unlockLevel)
        {
            heroName.gameObject.SetActive(false);
            heroLevel.gameObject.SetActive(false);
            tipLabel.gameObject.SetActive(true);

            int userLevel = StaticData.playerData.level;
            if(unlockLevel > userLevel)
            {
                emptyIcon.SetActive(true);
                lockLogo.SetActive(true);
                addLogo.SetActive(false);
                string str = TableTextBlo.GetText("x_level_open").Replace("\\r", "\r").Replace("\\n", "\n");
                tipLabel.text = string.Format(str, unlockLevel);
            }
            else
            {
                addLogo.SetActive(true);
                lockLogo.SetActive(false);
                emptyIcon.SetActive(true);
                tipLabel.text = TableTextBlo.GetText("add_hero").Replace("\\r", "\r").Replace("\\n", "\n");
            }
            
            if (heroIcon != null)
            {
                heroIcon.gameObject.SetActive(false);
            }
        }
    }
}
